# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=17.0.0.9000
pkgrel=8000
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=("any")
url="https://arrow.apache.org/"
license=("Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-curl" # for google-cloud-cpp bundled build
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-re2"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-brotli")
makedepends=("${MINGW_PACKAGE_PREFIX}-ccache"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=("staticlibs" "strip" "!buildflags")

# For installing from a local checkout, set source_dir to . and don't include
# a "source" param below
source_dir="$ARROW_HOME"
# else
# source_dir=apache-${_realname}-${pkgver}

# For released version:
#source=("https://archive.apache.org/dist/arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz")
#sha256sums=("ac2a77dd9168e9892e432c474611e86ded0be6dfe15f689c948751d37f81391a")
# For github dev version:
# Append `#commit=54b1b2f688e5e84b4c664b1e12a95f93b94ab2f3` to the URL to select a revision
# source=("${source_dir}"::"git+https://github.com/apache/arrow")
# sha256sums=("SKIP")
# source_dir="${APPVEYOR_BUILD_FOLDER}/${source_dir}"

cpp_build_dir=build-${CARCH}-cpp

pkgver() {
  # The only purpose of this here is to cause the job to error if the
  # version in pkgver is different from what is in r/DESCRIPTION
  grep Version "${source_dir}/r/DESCRIPTION" | cut -d " " -f 2
}

build() {
  ARROW_CPP_DIR="${source_dir}/cpp"
  [[ -d ${cpp_build_dir} ]] && rm -rf ${cpp_build_dir}
  mkdir -p ${cpp_build_dir}
  pushd ${cpp_build_dir}

  # We use static cURL in google-cloud-cpp. If we can use cURL's CMake
  # package, we don't need to specify CURL_STATICLIB explicitly. But
  # we don't have cURL's CMake package. We need to use CXXFLAGS
  # instead of ARROW_CXXFLAGS because ARROW_CXXFLAGS aren't passed to
  # ExternProjects.
  export CXXFLAGS="${CXXFLAGS} -DCURL_STATICLIB"
  # The Rtools libutf8proc is a static lib, but Findutf8proc.cmake doesn't
  # set the appropriate compiler definition.
  export CPPFLAGS="-DUTF8PROC_STATIC"

  # CMAKE_UNITY_BUILD is set to OFF as otherwise some compute functionality
  # segfaults in tests

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
    ${ARROW_CPP_DIR} \
    -G "MSYS Makefiles" \
    -DARROW_ACERO=ON \
    -DARROW_BUILD_SHARED=OFF \
    -DARROW_BUILD_STATIC=ON \
    -DARROW_BUILD_UTILITIES=OFF \
    -DARROW_COMPUTE=ON \
    -DARROW_CSV=ON \
    -DARROW_DATASET=ON \
    -DARROW_FILESYSTEM=ON \
    -DARROW_GCS=ON \
    -DARROW_HDFS=OFF \
    -DARROW_JEMALLOC=OFF \
    -DARROW_JSON=ON \
    -DARROW_LZ4_USE_SHARED=OFF \
    -DARROW_MIMALLOC=ON \
    -DARROW_PACKAGE_PREFIX="${MINGW_PREFIX}" \
    -DARROW_PARQUET=ON \
    -DARROW_S3=ON \
    -DARROW_SNAPPY_USE_SHARED=OFF \
    -DARROW_USE_GLOG=OFF \
    -DARROW_UTF8PROC_USE_SHARED=OFF \
    -DARROW_VERBOSE_THIRDPARTY_BUILD=ON \
    -DARROW_WITH_LZ4=ON \
    -DARROW_WITH_RE2=ON \
    -DARROW_WITH_SNAPPY=ON \
    -DARROW_WITH_ZLIB=ON \
    -DARROW_WITH_ZSTD=ON \
    -DARROW_WITH_BROTLI=ON \
    -DARROW_WITH_BZ2=ON \
    -DARROW_ZSTD_USE_SHARED=OFF \
    -DARROW_CXXFLAGS="${CPPFLAGS}" \
    -DCMAKE_BUILD_TYPE="release" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_UNITY_BUILD=OFF \
    -DCMAKE_VERBOSE_MAKEFILE=ON

  make -j3
  popd
}

package() {
  make -C ${cpp_build_dir} DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  for pc in *.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i $pc
  done
  popd
}
